package com.quan.system.common.tree;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 树状菜单工具类
 * @author yhaoquan
 *
 */
public class MenuTreeGridUtil {

	public static Map<String, Object> mapArray = new LinkedHashMap<String, Object>();

	public List<MenuTreeNode> menuCommon;
	public List<Object> list = new ArrayList<Object>();

	public List<Object> tree(List<MenuTreeNode> menu) {
		this.menuCommon = menu;
		for (MenuTreeNode node : menu) {
			Map<String, Object> mapArr = new LinkedHashMap<String, Object>();
			if (node.getPid() == 0) {
				setTreeMap(mapArr, node);
				list.add(mapArr);
			}
		}
		return list;
	}

	public List<?> menuChild(Long id) {
		List<Object> lists = new ArrayList<Object>();
		for (MenuTreeNode a : menuCommon) {
			Map<String, Object> childArray = new LinkedHashMap<String, Object>();
			if (a.getPid() == id) {
				setTreeMap(childArray, a);
				lists.add(childArray);
			}
		}
		return lists;
	}

	private void setTreeMap(Map<String, Object> mapArr, MenuTreeNode node) {
		// 可追加其他属性
		mapArr.put("id", node.getId());
		mapArr.put("pid", node.getPid());
		mapArr.put("name", node.getName());
		mapArr.put("url", node.getUrl());
		mapArr.put("method", node.getMethod());
		mapArr.put("icon", node.getIcon());
		mapArr.put("sort", node.getSort());
		mapArr.put("status", node.getStatus());
		mapArr.put("ctime", node.getCtime());
		mapArr.put("utime", node.getUtime());
		mapArr.put("permission", node.getPermission());
//		List<?> childrens = menuChild(node.getId());
//		if (childrens.size() > 0) {
//			mapArr.put("hasChildren", true);
//		} else {
//			mapArr.put("hasChildren", false);
//		}
		mapArr.put("children", menuChild(node.getId()));
	}

	public static void main(String[] args) {


	}

}
